package crawl.gov;

import java.net.URI;

import crawl.Scheme;
import crawl.URIFilter;

import utils.Logger;

public class GovHTMLFilter implements URIFilter
{
    public boolean accept(URI uri)
    {
        if (!Scheme.fromURI(uri).equals(Scheme.HTTP))
            return false;
        if (!uri.getHost().toLowerCase().equals("www.gov.cn"))
            return false;
        String path = uri.getRawPath().toLowerCase();
        if (!path.endsWith(".html") && !path.endsWith(".htm")
                && !path.endsWith("/"))
            return false;
        String specificPart = uri.getRawSchemeSpecificPart();
        if (specificPart.contains("?"))
            return false;
        return true;
    }

    public URI normalize(URI oldURI, URI newURI)
    {
        newURI = oldURI.resolve(newURI);
        newURI = newURI.normalize();
        try
        {
            String fileName = newURI.getPath();
            if (fileName != null && !fileName.isEmpty())
            {
                fileName = fileName.substring(
                        newURI.getPath().lastIndexOf("/") + 1).toLowerCase();
                if (fileName.equals("index.html")
                        || fileName.equals("index.htm")
                        || fileName.equals("default.html")
                        || fileName.equals("default.htm"))
                {
                    newURI = new URI(newURI.toString().toLowerCase().replace(
                            fileName, ""));
                }
            }
        } catch (Exception e)
        {
            Logger.logException(e);
            Logger.logException(newURI.toString());
        }
        return newURI;
    }
}
